//
//  AirMonitoringController.m
//  YSSmartHome
//
//  Created by yanghao on 2017/3/12.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "AirMonitoringController.h"
#import "ZTDevice.h"
#import "WaveView.h"
#import "AirAttributedView.h"
#import "AirContentScrollView.h"
#import "ZTAirMonitoringTopCell.h"
#import "ZTAirMonitoringBottomCell.h"
#import "YSCommon.h"
#import "ZTControlTopView.h"
#import "CommonUtils.h"
#import "DataBaseManager.h"

static NSString* const ZTAirMonitoringTopCellIdentifier = @"ZTAirMonitoringTopCellIdentifier";
static NSString* const ZTAirMonitoringBottomCellIdentifier = @"ZTAirMonitoringBottomCellIdentifier";
@interface AirMonitoringController ()
<
UITableViewDelegate,
UITableViewDataSource,
ZTAirMonitoringTopCellDelegate,
ZTAirMonitoringBottomCellDelegate,
DataManagerRefreshDelegate
>
@property (nullable, nonatomic, strong) AirContentScrollView* mScrollView;

@property (nullable, nonatomic, strong) UITableView* tableView;
@property (nullable, nonatomic, strong) UIColor* color;
@property (nonatomic, strong) ZTControlTopView *topView;
@end

@implementation AirMonitoringController

- (void)dealloc
{
    [kDataManager removeRefreshDelegate:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.mScrollView];
    
    [self.mScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).offset(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
    [kDataManager addRefreshDelegate:self];
    __weak typeof(self)wself = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        __strong typeof(wself)sself = wself;
        [sself sendAirData:YES];
//        [kDataBaseManager  loadAirDataWithApsn:sself.device.apsn luid:sself.device.luid type:0x01 complete:^(NSArray * array) {
//            sself.mScrollView.pmArray = array;
//        }];
//        [kDataBaseManager  loadAirDataWithApsn:sself.device.apsn luid:sself.device.luid type:0x02 complete:^(NSArray * array) {
//            sself.mScrollView.formaldehydeArray = array;
//        }];
//        [kDataBaseManager  loadAirDataWithApsn:sself.device.apsn luid:sself.device.luid type:0x03 complete:^(NSArray * array) {
//            sself.mScrollView.tempArray = array;
//        }];
        
    });
    
    [self setData];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hiddenSplitView:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
     [self hiddenSplitView:NO];
}

- (void)changeColor
{
//    self.color = RGBColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256));
//    [self.tableView reloadData];
}


- (void)sendAirData:(BOOL)isEnable
{
    XAITLV* tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_BOOL bytes:&isEnable length:sizeof(isEnable)];
    [kMQTTSessionManager.xai sendCMD:0x01 toApsn:self.device.apsn toLuid:self.device.luid datas:@[tlv]];
}

- (void)deviceRefresh:(DataManager *)manager callbackType:(DeviceCallbackType)callbackType
{
    switch (callbackType) {
        case DeviceCallbackTypeStatus:
        {
            [self setData];
        }
            break;
            
        default:
            break;
    }
}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    if ( 0 == indexPath.row)
    {
        ZTAirMonitoringTopCell* cell = [tableView dequeueReusableCellWithIdentifier:ZTAirMonitoringTopCellIdentifier forIndexPath:indexPath];
        cell.backgroundColor = self.view.backgroundColor;
        cell.delegate = self;
        cell.device = self.device;
        return cell;
    }
    else if (1 == indexPath.row)
    {
        ZTAirMonitoringBottomCell* cell = [tableView dequeueReusableCellWithIdentifier:ZTAirMonitoringBottomCellIdentifier forIndexPath:indexPath];
        cell.backgroundColor = self.view.backgroundColor;
        cell.delegate = self;
        return cell;
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ZTAirMonitoringTopCellDelegate

- (void)setDevice:(ZTAirMonitorDevice *)device
{
    _device = device;
}

- (void)setData
{
    dispatch_async(dispatch_get_main_queue(), ^{
        int colorHex = [CommonUtils evaluateColor:0xFF006fc3 endValue:0xFFef4b3a fraction: self.device.pmValue / 500.0];
        UIColor* color = UIColorFromHex(colorHex);
        self.navigationBar.barTintColor = color;
        NSString *strName = [self.device getDeviceName];
        self.titleItem.title = strName;
        self.mScrollView.device = self.device;
    });
}

- (void)airMonitoringTopCellRecordTapped:(ZTAirMonitoringTopCell *)cell
{
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
}

#pragma mark - ZTAirMonitoringBottomCellDelegate
- (void)airMonitoringBottomCellRecordTapped:(ZTAirMonitoringBottomCell *)cell
{
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
}

#pragma mark getters and setters
- (AirContentScrollView *)mScrollView
{
    if (!_mScrollView) {
        _mScrollView = [[AirContentScrollView alloc]init];
        _mScrollView.showsVerticalScrollIndicator = NO;
        _mScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _mScrollView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[ZTAirMonitoringTopCell class] forCellReuseIdentifier:ZTAirMonitoringTopCellIdentifier];
        [_tableView registerClass:[ZTAirMonitoringBottomCell class] forCellReuseIdentifier:ZTAirMonitoringBottomCellIdentifier];
        _tableView.pagingEnabled = YES;
        _tableView.rowHeight = kScreenHeight - 64;
    }
    return _tableView;
}
@end
